#pragma once

#include <pthread.h>

//RAII 风格的锁
//创建锁的对象，自动加锁
//对象销毁时，自动释放锁
class Mutex
{
public:
    Mutex(pthread_mutex_t* pmtx)
        :_pmtx(pmtx) 
    {}

    //加锁
    void lock()
    {
        pthread_mutex_lock(_pmtx);
    }

    //解锁
    void unlock()
    {
        pthread_mutex_unlock(_pmtx);
    }

private:
    pthread_mutex_t *_pmtx;
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t *pmtx)
        :_mtx(pmtx)
    {
        //调用加锁
        _mtx.lock();
    }

    ~LockGuard()
    {
        //调用解锁
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};